//
//  UserCenterViewController.m
//  CarPark
//
//  Created by oceans on 2019/7/16.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "UserCenterViewController.h"
#import <UIImageView+WebCache.h>
//#import <SDWebImage/UIImageView+WebCache.h>
#import "TipManager.h"
#import "XiaoXiViewController.h"

@interface UserCenterViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *companyLabel;
@property (strong, nonatomic) UILabel *chipLabel;
@property (strong, nonatomic) UILabel *reviewLabel;
@end

@implementation UserCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    if (@available(iOS 11.0, *)) {
        _tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:kuser_center paras:nil showProgress:NO successful:^(id  _Nullable result) {
        
        NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", result[@"datas"][@"rfid_nums"]] attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:40.0], NSForegroundColorAttributeName: [UIColor blackColor]}];
        [string1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n今日安装/更换数量" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.0], NSForegroundColorAttributeName: [UIColor blackColor]}]];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        [string1 addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string1.length)];
        weakself.chipLabel.attributedText = string1;
        
        NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", result[@"datas"][@"verify_nums"]] attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:40.0], NSForegroundColorAttributeName: [UIColor blackColor]}];
        [string2 appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n今日审核" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.0], NSForegroundColorAttributeName: [UIColor blackColor]}]];
        NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
        style2.alignment = NSTextAlignmentCenter;
        [string2 addAttribute:NSParagraphStyleAttributeName value:style2 range:NSMakeRange(0, string2.length)];
        weakself.reviewLabel.attributedText = string2;
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-XTabBarHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
    
    _tableview.tableFooterView = [[UIView alloc] init];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, XNavigationHeight+210.0)];
    view.backgroundColor = [UIColor whiteColor];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, XStatusBarHeight)];
    line.backgroundColor = COLOR(201, 21, 29, 1);
    //COLOR(79, 59, 63, 1);
    [view addSubview:line];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, XStatusBarHeight, ScreenWidth, 144.0)];
    header.backgroundColor = COLOR(201, 21, 29, 1);
    //COLOR(79, 59, 63, 1);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [button setImage:[UIImage imageNamed:@"xiaoxi"] forState:UIControlStateNormal];
    button.frame = CGRectMake(ScreenWidth-44.0, 0, 44.0, 44.0);
    [button addTarget:self action:@selector(messageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:button];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 0.0, ScreenWidth-120.0, 44.0)];
    _titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text = @"个人中心";
    [header addSubview:_titleLabel];
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 60, 60, 60)];
    [header addSubview:_imageView];
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(95.0, 63.0, ScreenWidth-100.0, 22.0)];
    _nameLabel.font = [UIFont boldSystemFontOfSize:17.0];
    _nameLabel.textColor = [UIColor whiteColor];
    [header addSubview:_nameLabel];
    
    _companyLabel = [[UILabel alloc] initWithFrame:CGRectMake(95.0, 95.0, ScreenWidth-100.0, 22.0)];
    _companyLabel.font = [UIFont boldSystemFontOfSize:17.0];
    _companyLabel.textColor = [UIColor whiteColor];
    [header addSubview:_companyLabel];
    
    _chipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 144.0+XStatusBarHeight, ScreenWidth*0.5, 100.0)];
    _chipLabel.numberOfLines = 0;
    [view addSubview:_chipLabel];
    
    _reviewLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth*0.5, 144.0+XStatusBarHeight, ScreenWidth*0.5, 100.0)];
    _reviewLabel.numberOfLines = 0;
    [view addSubview:_reviewLabel];
    
    if ([UserModel shared].hasLogin) {
        [_imageView sd_setImageWithURL:[NSURL URLWithString:[UserModel shared].loginUser.avatar]];
        _nameLabel.text = [UserModel shared].loginUser.name;
        _companyLabel.text = [UserModel shared].loginUser.company.name;
    }
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 244.0+XStatusBarHeight, ScreenWidth, 10.0)];
    line2.backgroundColor = COLOR(242, 242, 242, 1);
    [view addSubview:line2];
    
    [view addSubview:header];
    
    _tableview.tableHeaderView = view;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    
    [self loadData];
    
    if ([UserModel shared].hasLogin) {
        [_imageView sd_setImageWithURL:[NSURL URLWithString:[UserModel shared].loginUser.avatar]];
        _nameLabel.text = [UserModel shared].loginUser.name;
        _companyLabel.text = [UserModel shared].loginUser.company.name;
        [_tableview reloadData];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)messageButtonPressed:(UIButton *)sender {
    
    XiaoXiViewController *con = [[XiaoXiViewController alloc] init];
    [Utility pushViewController:con];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的手机";
        cell.detailTextLabel.text = nil;
    }else if (indexPath.row == 1) {
        cell.textLabel.text = @"清除缓存";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f M", [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0];
    }else {
        cell.textLabel.text = @"退出登录";
        cell.detailTextLabel.text = nil;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 2) {
        
        defweakself
        UIAlertController *con = [UIAlertController alertControllerWithTitle:@"退出登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [con addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [con addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakself logout];
        }]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakself presentViewController:con animated:YES completion:nil];
        });
    }else if (indexPath.row == 1) {
        
        defweakself
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [TipManager showTip:@"清理完成"];
            [weakself.tableview reloadData];
        }];
    }
}

- (void)logout {
    
    [[UserModel shared] logoutSuccessBlock:^{
        [Utility showLogin];
    }];
}

@end
